package com.example.service_msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.service_msm.service.MsmService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;

import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teautil.*;
import com.aliyun.teautil.models.*;

@Service
public class MsmServiceImpl implements MsmService {

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    @Override  //发送阿里云的测试短信，只能给已经绑定的手机号码发送
    public boolean send(HashMap<String, Object> param, String phone) {
        try {
            if (StringUtils.isEmpty(phone)) return false;
            com.aliyun.dysmsapi20170525.Client client = createClient("LTAI5tDQMjAsDJbnB3gMR2fb", "xe49sC9I5p309AhMWO1UDhVyVwmEAn");
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName("阿里云短信测试")
                    .setTemplateCode("SMS_154950909")
                    .setPhoneNumbers(phone)
                    .setTemplateParam("{\"code\":\"" + param.get("code") + "\"}");
            RuntimeOptions runtime = new RuntimeOptions();

            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            String message = sendSmsResponse.getBody().getMessage();
            if (message.equalsIgnoreCase("ok")) {
                return true;
            } else {
                return false;
            }
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            return false;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            return false;
        }
    }

//    @Override
//    public boolean send(HashMap<String, Object> param, String phone) {
//        if(StringUtils.isEmpty(phone)) return false;
//
//        DefaultProfile profile =
//                DefaultProfile.getProfile("default", "LTAI5tDQMjAsDJbnB3gMR2fb", "xe49sC9I5p309AhMWO1UDhVyVwmEAn");
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        //设置参数
//        CommonRequest request = new CommonRequest();
//        //request.setProtocol(ProtocolType.HTTPS);
//        request.setMethod(MethodType.POST);
//        request.setDomain("dysmsapi.aliyuncs.com");
//        request.setVersion("2017-05-25");
//        request.setAction("SendSms");
//
//        request.putQueryParameter("PhoneNumbers", phone);
//        request.putQueryParameter("SignName", "我的谷粒在线教育网站");//TODO 要改成自己的签名名字
//        request.putQueryParameter("TemplateCode", "SMS_242962705");
//        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
//
//        try {
//            CommonResponse response = client.getCommonResponse(request);
//            System.out.println(response.getData());
//            return response.getHttpResponse().isSuccess();
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
}
